def sort_and_sorted():
    my_list = "This is a test string from Andrew".split()
    print(my_list)

    my_list.sort()  # 升序
    print(my_list)
    my_list.sort(reverse=True)  # 降序
    print(my_list)

    def change_lower(my_str: str):
        """
        将字符串的所有字母都转换成小写并返回
        """
        return my_str.lower()

    # key=函数名，类似于C++的Compare自定义函数，自定义比较方法
    my_list.sort(key=change_lower)
    print(my_list)

    # sorted方法不会改变已有的列表，而是返回一个改变后的副本
    new_list = sorted(my_list)
    print(new_list)
    print(my_list)
    # 地址不同，返回的是副本
    print(id(my_list))  # 1728108286336
    print(id(new_list))  # 1728110580288


def sorted_tuple_list():
    student_tuples_list = [
        ('jane', 'B', 12),
        ('john', 'A', 15),
        ('dave', 'B', 10),
    ]
    print(student_tuples_list)
    # lambda表达式，就是匿名函数，匿名函数好处，提高编写效率，提高阅读速度
    # lambda x: x[i]    x是列表中的元素元组，x[i]是元组中下标为2的基础元素，以该元素作为比较依据
    new_list_one = sorted(student_tuples_list, key=lambda x: x[0])
    print(new_list_one)
    new_list_two = sorted(student_tuples_list, key=lambda x: x[1])
    print(new_list_one)
    new_list_three = sorted(student_tuples_list, key=lambda x: x[2])
    print(new_list_three)
    new_list_four = sorted(student_tuples_list, key=lambda x: x[2], reverse=True)
    print(new_list_four)


class Student:
    def __init__(self, name, grade, age):
        self.name = name
        self.grade = grade
        self.age = age

    def __repr__(self):
        """
        相对于__str__来说，更方便，可以返回非字符串类型
        __str__和__repr__，只会写一种
        """
        return repr((self.name, self.grade, self.age))


def sorted_class_list():
    student_class_list = [
        Student('john', 'A', 15),
        Student('jane', 'B', 12),
        Student('dave', 'B', 10),
    ]
    print(student_class_list)
    # lambda x: x.对象属性    x是列表中的对象元素，以指定对象属性作为比较依据
    print(sorted(student_class_list, key=lambda x: x.name))
    print(sorted(student_class_list, key=lambda x: x.grade))
    print(sorted(student_class_list, key=lambda x: x.age))


from operator import itemgetter, attrgetter


def sorted_operator():
    student_tuples_list = [
        ('jane', 'B', 12),
        ('john', 'A', 15),
        ('dave', 'B', 10),
    ]
    student_class_list = [
        Student('john', 'A', 15),
        Student('jane', 'B', 12),
        Student('dave', 'B', 10),
    ]
    print('使用operator系列')
    # 列表元素是元组时，用元组下标直接指定比较依据
    print(sorted(student_tuples_list, key=itemgetter(0)))
    print(sorted(student_tuples_list, key=itemgetter(1)))
    print(sorted(student_tuples_list, key=itemgetter(2)))
    # 列表元素是对象时，用对象属性直接指定比较依据
    print(sorted(student_class_list, key=attrgetter("name")))
    print(sorted(student_class_list, key=attrgetter("grade")))
    print(sorted(student_class_list, key=attrgetter("age")))

    print('使用operator系列,多列排序')
    # itemgetter(i, j)：先比较下标为i的元素，若相等，则再比较下标为j的元素
    print(sorted(student_tuples_list, key=itemgetter(1, 2)))
    print(sorted(student_tuples_list, key=lambda x: (x[1], x[2])))
    print(sorted(student_tuples_list, key=lambda x: (x[1], -x[2])))  # 第一列升序，第二列降序
    print(sorted(student_class_list, key=attrgetter('grade', 'age')))


def sorted_is_stability():
    print('查看排序稳定性')
    data = [('red', 1), ('blue', 1), ('red', 2), ('blue', 2)]
    print(sorted(data, key=itemgetter(0)))
    # [('blue', 1), ('blue', 2), ('red', 1), ('red', 2)]
    # sorted是稳定的


def sorted_dict():
    my_dict = {'Li': ['M', 7],
               'Zhang': ['E', 2],
               'Wang': ['P', 3],
               'Du': ['C', 2],
               'Ma': ['C', 9],
               'Zhe': ['H', 7]}
    # lambda x: x[0]  按键排序
    print(sorted(my_dict.items(), key=lambda x: x[0]))
    # lambda x: x[1][1]  按值的下标为1的元素排序
    print(sorted(my_dict.items(), key=lambda x: x[1][1]))
    my_dict_list = [
        {"name": "Bob", "wins": 10, "losses": 3, "rating": 75.00},
        {"name": "David", "wins": 3, "losses": 5, "rating": 57.00},
        {"name": "Carol", "wins": 4, "losses": 5, "rating": 57.00},
        {"name": "Patty", "wins": 9, "losses": 3, "rating": 71.48}]
    print(sorted(my_dict_list, key=lambda x: x['rating']))
    print(sorted(my_dict_list, key=itemgetter("rating", "name")))


if __name__ == '__main__':
    # sort_and_sorted()
    # sorted_tuple_list()
    # sorted_class_list()
    # sorted_operator()
    # sorted_is_stability()
    sorted_dict()
